﻿using System;
using System.Collections.Generic;
using System.Text;
using FieldDevice.ModBus;
using FieldLib;

namespace FieldDevice.Devices
{
    class DeviceREF615:Device
    {

        public DeviceREF615(byte address)
            : base(DeviceCategory.REF615,address)
        { 
        }
        public void Initialize()
        {
            base.RequestRegister(ADDR_Ia, 3);
            this.RequestRegister(ADDR_LED, 8,ModBus.ModBusFunction.READINPUTSTATUS);
            this.RequestRegister(X120_Input1, 40, ModBus.ModBusFunction.READINPUTSTATUS);
            
        }

        protected void RequestRegister(ushort registeraddress, ushort datanumber,byte fuction)
        {
            ModBusPacket modframe = new ModBusPacket();
            modframe.Function = fuction; 
            modframe.SlaveAddress = (byte)this.ModbusAddress;

            modframe.DataAddressHi = (byte)(registeraddress >> 8);
            modframe.DataAddressLow = (byte)(registeraddress & 0x00FF);
            modframe.DataNumberHi = 0;
            modframe.DataNumberLow = (byte)(datanumber & 0x00FF);
            this.requests.Add(modframe);
        }


        public const ushort ADDR_Ia = 2000;             //A相电流
        public const ushort ADDR_Ib = 2001;             //B相电流
        public const ushort ADDR_Ic = 2002;             //C相电流
        //public const ushort ADDR_LED1 = 2768;       //过流保护
        //public const ushort ADDR_LED2 = 2769;       //零流保护
        //public const ushort ADDR_LED3 = 2770;       //速断
        //public const ushort ADDR_LED4 = 2778;       //超温跳闸
        public const ushort ADDR_LED = 2768;            //灯状态首地址
        public const ushort X120_Input1 = 3200;          //开关状态输入首地址
        public const ushort X120_Input2 = 3202;         //压力异常
        public const ushort X120_Input3 = 3204;         //负荷开关合位（引致计量柜）
        public const ushort X120_Input4 = 3206;         //断路器储能
        public const ushort X110_Input1 = 3216;         //隔离刀合闸
        public const ushort X110_Input2 = 3218;         //隔离刀分闸
        public const ushort X110_Input3 = 3220;         //接地刀分闸
        public const ushort X110_Input4 = 3222;         //接地刀合闸
        public const ushort X110_Input5 = 3224;         //断路器合闸
        public const ushort X110_Input6 = 3226;         //断路器分闸
        public const ushort X110_Input7 = 3228;         //变压器高温
        public const ushort X110_Input8 = 3230;         //变压器超温

        public const ushort BIO_Card_X110 = 3216;           

        byte[] RegistersValue = new byte[522];

        protected override float Translate(byte[] value, int begin, ushort registeraddress)
        {
            short tmpValue = value[begin];
            tmpValue <<= 8;
            tmpValue += value[begin + 1];
            switch (registeraddress)
            { 
                case ADDR_Ia:
                    RecordRead("Ia", tmpValue);
                    break;
                case ADDR_Ib:
                    RecordRead("Ib", tmpValue);
                    break;
                case ADDR_Ic:
                    RecordRead("Ic", tmpValue);
                    break;
              
                default:
                    break;
            
            }
            return 0.0f;
        }

        public override void TranslateRegister(ModBusPacket request, byte[] response/*, ushort DataAddr*/ )
        {

            CheckOut(response);
            switch (response[1])
            {
                case ModBus.ModBusFunction.READINPUTSTATUS:
                    for(int i=0;i<response[2];i++)
                    {
                        ProtectCondition(request.DataAddress+i, response[i + 2]);
                    }
                    break;
                default:
                    byte datalength = response[2];
                    int DataNumber = datalength / 2;
                    for (int i = 0; i < DataNumber; i++)
                    {
                        Translate(response, 3 + i * 2, (ushort)(request.DataAddress + i));
                    }
                    break;
            }
        }

        public void ProtectCondition(int dataAddr, byte data)
        {
            switch (dataAddr)
            {
                case ADDR_LED:
                    if (Convert.ToBoolean(data & 0x0001) == true)
                        RecordRead("OverCurrentProtect", 1);            //过流保护
                    else
                        RecordRead("OverCurrentProtect", 0);
                    if (Convert.ToBoolean(data & 0x0002) == true)
                        RecordRead("ZeroFlowProtect", 1);               //零流保护
                    else
                        RecordRead("ZeroFlowProtect", 0);
                    if (Convert.ToBoolean(data & 0x0004) == true)
                        RecordRead("QuickBreak", 1);                       //速断
                    else
                        RecordRead("QuickBreak", 0);
                    if (Convert.ToBoolean(data & 0x0008) == true)
                        RecordRead("OvertemperatureTrip", 1);               //超温跳闸
                    else
                        RecordRead("OvertemperatureTrip", 0);
                    break;
                case X120_Input1:
                    if (Convert.ToBoolean(data & 0x0001) == true)
                        RecordRead("Vol_Tran_IsolationKnife_Connect", 1);            //隔离刀合闸（引致变压柜）
                    else
                        RecordRead("Vol_Tran_IsolationKnife_Connect", 0);
                     if (Convert.ToBoolean(data & 0x0004) == true)
                         RecordRead("PressureAnomaly", 1);               //压力异常
                    else
                         RecordRead("PressureAnomaly", 0);
                    if (Convert.ToBoolean(data & 0x0010) == true)
                        RecordRead("LoadSwitch_Connect", 1);             //负荷开关合位（引致测量柜）
                    else
                        RecordRead("LoadSwitch_Connect", 0);
                    break;
                case X110_Input1:
                     if (Convert.ToBoolean(data & 0x0001) == true)
                           RecordRead("Iso_Knife_Connect", 1);            //隔离刀合闸
                    else
                           RecordRead("Iso_Knife_Connect", 0);
                     if (Convert.ToBoolean(data & 0x0004) == true)
                         RecordRead("Iso_Knife_Cutoff", 1);               //隔离刀分闸
                    else
                         RecordRead("Iso_Knife_Cutoff", 0);
                    if (Convert.ToBoolean(data & 0x0010) == true)
                        RecordRead("GroundedKnifeCutoff", 1);             //接地刀分闸
                    else
                        RecordRead("GroundedKnifeCutoff", 0);
                    if (Convert.ToBoolean(data & 0x0040) == true)
                        RecordRead("GroundedKnifeConnect", 1);               //接地刀合闸
                    else
                        RecordRead("GroundedKnifeConnect", 0);
                    break;
                case X110_Input1+1:
                    if (Convert.ToBoolean(data & 0x0001) == true)
                        RecordRead("CB_Connect", 1);                        //断路器合闸
                    else
                        RecordRead("CB_Connect", 0);
                     if (Convert.ToBoolean(data & 0x0004) == true)
                         RecordRead("CB_Cutoff", 1);                         //断路器分闸
                    else
                         RecordRead("CB_Cutoff", 0);
                    if (Convert.ToBoolean(data & 0x0010) == true)
                        RecordRead("Transformer_HighTemp", 1);         //变压器高温
                    else
                        RecordRead("Transformer_HighTemp", 0);
                    if (Convert.ToBoolean(data & 0x0040) == true)
                        RecordRead("Transformer_OvereTemp", 1);              //变压器超温
                    else
                        RecordRead("Transformer_OvereTemp", 0);
                    break;
                default:
                    break;
            }
        }

        public void ReadProCondition(ushort DataAddr, ushort DataNumber)
        {
            ModBusPacket packet = new ModBusPacket();
            packet.SlaveAddress = this.ModbusAddress;
            packet.Function = FieldDevice.ModBus.ModBusFunction.READINPUTSTATUS;
            packet.DataAddressHi = (byte)(DataAddr >> 8);
            packet.DataAddressLow = (byte)(DataAddr & 0xff);
            packet.DataNumberHi = (byte)(DataNumber >> 8);
            packet.DataNumberLow = (byte)(DataNumber & 0xff);
            DeviceManager.GlobalDeviceManager.ModBusIO.Send(this, packet);
            RegistersValue = DeviceManager.GlobalDeviceManager.ModBusIO.Receive(this);
           
        }

    }
}
